package com.wpml.cloud.classroom.interceptor;

import cn.hutool.core.util.RandomUtil;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 日志输出时添加 logId 便于日志跟踪
 * <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss:SSS} [ LOGID:%X{logId} ] [%t] [%p] - %l - %m%n" />
 * @Date 2021/5/13 10:08
 * @Author wangpei
 **/
public class LogInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String logId = RandomUtil.randomString(RandomUtil.BASE_CHAR_NUMBER, 18);
        ThreadContext.put("logId", logId);
        return true;
    }
}
